package com.ttxxly.reader.ui.fragment.bookshelf;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ttxxly.reader.GlideApp;
import com.ttxxly.reader.R;
import com.ttxxly.reader.entity.Bookshelf;
import com.ttxxly.reader.entity.Const;
import com.ttxxly.reader.ui.finish.FinishActivity;
import com.ttxxly.reader.ui.read.ReadActivity;

/**
 * Created by Administrator on 2/26/2018.
 */

public class BookshelfAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Bookshelf Bookshelf;
    private ViewGroup mContainer = null;

    public BookshelfAdapter(Bookshelf Bookshelf, ViewGroup container) {
        this.Bookshelf = Bookshelf;
        mContainer = container;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(
                mContainer.getContext()).inflate(R.layout.item_bookshelf, parent,
                false);
        return new BooksHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        BooksHolder booksHolder = (BooksHolder) holder;
        //绑定数据

        booksHolder.title.setText(Bookshelf.getBooks().get(position).getBookTitle());
        booksHolder.author.setText(Bookshelf.getBooks().get(position).getAuthor());
        booksHolder.latestChapter.setText(Bookshelf.getBooks().get(position).getReadChapterTitle());
        //封面
        GlideApp
                .with(mContainer)
                .load(Const.IMG_BASE_URL + Bookshelf.getBooks().get(position).getCover())
                .placeholder(R.drawable.cover_default)
                .error(R.drawable.cover_default)
                .into( booksHolder.cover);

        //Item 点击事件
        booksHolder.mrlBookshelf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContainer.getContext(), ReadActivity.class);
                mContainer.getContext().startActivity(intent);
            }
        });
        //Item 长按事件
        booksHolder.mrlBookshelf.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Toast.makeText(mContainer.getContext(), "长按事件",
                        Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    }

    class BooksHolder extends RecyclerView.ViewHolder {


        private final TextView title;
        private final TextView author;
        private final TextView latestChapter;
        private final ImageView cover;
        private final RelativeLayout mrlBookshelf;

        BooksHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.tvBookrackBookTitle);
            author = itemView.findViewById(R.id.tvBookrackBookAuthor);
            latestChapter = itemView.findViewById(R.id.tvBookrackBookShort);
            cover = itemView.findViewById(R.id.ivBookrackBookCover);
            mrlBookshelf = itemView.findViewById(R.id.rlBookshelf);
        }
    }

    /**
     * 获取书架上的书籍总数
     *
     * @return
     */
    @Override
    public int getItemCount() {
        return Bookshelf.getBooks().size();
    }
}
